limsLocationBoard.sampleStorageBoardChoosePage = {

    getSampleGrid: function () {
        var _this = this;
        var requestData = _this.param;
        return {
            type: 'grid',
            id: 'lims-storage-location-board-sample-list-grid',
            url: limsLocationBoard.sampleUrl + '/queries/rows',
            requestData: requestData,
            page: true,
            columns: limsLocationBoard.getStorageSampleListColumns(),
            onCellClick: function (field, row) {
                field === 'ext$.batchcode' && row.ext$.batchid && limsLocationBoard.loadBatchDetailPage(row)
            },
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0);
                }
            },
            toolbar: [{
                type: 'form',
                id: 'sample-storage-form',
                fields: [{
                    field: 'assignType',
                    title: 'T_LIMS_LOCATION.ASSIGNTYPE',
                    refreshGrid: false,
                    inputWidth: 80,
                    type: 'select',
                    firstBlank: false,
                    category: 'sys',
                    param: {
                        codeCategoryId: 'assignType'
                    },
                    value: 'x',
                    onChange: function (field, value) {
                        this.setData({
                            startHole: ''
                        })
                    }
                }, {
                    field: 'startHole',
                    title: 'T_LIMS_LOCATION.STARTHOLE',
                    refreshGrid: false,
                    inputWidth: 80,
                    placeholder: '格式为一位字母加数字，例：A01'// 未生效
                }, {
                    field: 'sampleCode',
                    refreshGrid: false,
                    title: 'T_LIMS_SAMPLE.SAMPLECODE',
                    onKeyDown: function (keyCode, value) {
                        if (keyCode != '13') {
                            return;
                        }
                        let activedRow = Gikam.getComp('lims-storage-location-board-choose-list-grid').getActivedRow();
                        if (Gikam.isEmpty(activedRow)) {
                            return;
                        }
                        if (activedRow.freeHoleCount <= 0) {
                            Gikam.alert('板位置剩余不足，请选择其他板')
                            return
                        }

                        let startHole = Gikam.getComp('sample-storage-form').getData().startHole;
                        let row = 1;
                        let col = 1;
                        if (!Gikam.isEmpty(startHole)) {
                            // 校验孔位是否满足格式
                            let regex = "^[a-zA-Z][0-9]+$";
                            if (!startHole.match(regex)) {
                                Gikam.alert('起始孔位须为【一位字母+数字】的格式，例：【A01】');
                                return;
                            }
                            // 校验输入孔位是否正确
                            let rowNum = activedRow.rowNum;
                            let columnNum = activedRow.columnNum;
                            row = startHole.toUpperCase().charCodeAt(0) - 64;
                            col = startHole.substring(1).replace(/\b(0+)/gi, "");
                            if (row > rowNum || parseInt(col) > columnNum) {
                                Gikam.alert('输入孔位在板中不存在');
                                return;
                            }
                        }

                        let assignType = Gikam.getComp('sample-storage-form').getData().assignType;
                        var sampleData = Gikam.getComp('lims-storage-location-board-sample-list-grid').getData()
                        let choose = sampleData.filter(item => item.sampleCode === value);
                        if (Gikam.isEmpty(choose)) {
                            Gikam.alert('T_LIMS_LOCATION_BOARD.ASSIGN.NO_SAMPLE');
                            return;
                        }
                        if (Gikam.isNotEmpty(choose[0].boardId) || !Gikam.isEmpty(choose[0].locationId)) {
                            console.log(choose[0])
                            Gikam.alert('T_LIMS_LOCATION_BOARD.ASSIGN.SAMPLE_ASSIGNABLE');
                            return;
                        }

                        workspace.window.showMask();
                        Gikam.postText(limsLocationBoard.baseUrl + '/update-samples-batch',
                            Gikam.getJsonWrapper({
                                assignType: assignType,
                                row: row,
                                col: col
                            }, [null, [activedRow]], [null, choose])).done(function (message) {
                            if (message !== 'success') {
                                Gikam.alert(message);
                                return;
                            }
                            // Gikam.getComp('lims-storage-location-board-sample-list-grid').refreshRowById(choose[0].id);
                            Gikam.getComp('lims-storage-location-board-sample-list-grid').refresh();
                            Gikam.getComp('lims-storage-location-board-choose-list-grid').refreshRowById(activedRow.id);
                            _this.refreshHole(activedRow.id);
                        }).always(function () {
                            let searchForm = Gikam.getComp('sample-storage-form')
                            searchForm.setData({
                                // assignType: 'x',
                                sampleCode: '',
                            })// 分配位置成功后清除左上表格上面搜索框的内容
                            workspace.window.closeMask();
                        });
                    }
                }]
            }, {
                type: 'dropDownMenu',
                text: 'T_LIMS_LOCATION.TITLE.LOCATION',
                icon: 'select',
                items: [{
                    type: 'button',
                    text: 'LIMS.LOCATION.BATCH.ASSIGN_LOCATION',
                    icon: 'modify',
                    onClick: function () {
                        _this.batchAssignLocation();
                    }
                }, {
                    type: 'button',
                    text: 'LIMS.LOCATION.BATCH.CLEAN_LOCATION',
                    icon: 'remove-row',
                    onClick: function () {
                        _this.batchClearLocation();
                    }
                }]
            }]
        }
    },

    batchAssignLocation: function () {
        var _this = this;
        let activedRow = Gikam.getComp('lims-storage-location-board-choose-list-grid').getActivedRow();
        if (Gikam.isEmpty(activedRow)) {
            return;
        }
        var grid = Gikam.getComp('lims-storage-location-board-sample-list-grid');
        var selections = grid.getSelections();
        if (Gikam.isEmpty(selections)) {
            Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
            return;
        }
        if (activedRow.freeHoleCount <= 0) {
            Gikam.alert('板位置剩余不足，请选择其他板');
            return
        }

        let startHole = Gikam.getComp('sample-storage-form').getData().startHole;
        let row = 1;
        let col = 1;
        if (!Gikam.isEmpty(startHole)) {
            // 校验孔位是否满足格式
            let regex = "^[a-zA-Z][0-9]+$";
            if (!startHole.match(regex)) {
                Gikam.alert('起始孔位须为【一位字母+数字】的格式，例：【A01】');
                return;
            }
            // 校验输入孔位是否正确
            let rowNum = activedRow.rowNum;
            let columnNum = activedRow.columnNum;
            row = startHole.toUpperCase().charCodeAt(0) - 64;
            col = startHole.substring(1).replace(/\b(0+)/gi, "");
            if (row > rowNum || parseInt(col) > columnNum) {
                Gikam.alert('输入孔位在板中不存在');
                return;
            }
        }

        let assignType = Gikam.getComp('sample-storage-form').getData().assignType;

        let bool = selections.filter(s => !Gikam.isEmpty(s.boardId) || !Gikam.isEmpty(s.locationId));
        if (bool.length > 0) {
            Gikam.alert('T_LIMS_LOCATION_BOARD.ASSIGN.SAMPLE_ASSIGNABLE');
            return;
        }

        workspace.window.showMask();
        Gikam.postText(limsLocationBoard.baseUrl + '/update-samples-batch',
            Gikam.getJsonWrapper({
                assignType: assignType,
                row: row,
                col: col
            }, [null, [activedRow]], [null, selections])).done(function (message) {
            if (message !== 'success') {
                Gikam.alert(message);
                return;
            }
            // Gikam.getComp('lims-storage-location-board-sample-list-grid').refreshRowById(choose[0].id);
            Gikam.getComp('lims-storage-location-board-sample-list-grid').refresh();
            Gikam.getComp('lims-storage-location-board-choose-list-grid').refreshRowById(activedRow.id);
            _this.refreshHole(activedRow.id);
        }).always(function () {
            let searchForm = Gikam.getComp('sample-storage-form');
            searchForm.setData({
                // assignType: 'x',
                sampleCode: '',
            })// 分配位置成功后清除左上表格上面搜索框的内容
            workspace.window.closeMask();
        });
    },

    batchClearLocation: function () {
        var _this = this;
        var grid = Gikam.getComp('lims-storage-location-board-sample-list-grid');
        var selections = grid.getSelections();
        if (Gikam.isEmpty(selections)) {
            Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
            return;
        }
        Gikam.confirm('GIKAM.BUTTON.CONFIRM', Gikam.propI18N('LIMS.LOCATION.BUTTON.BATCH.CLEAN_LOCATION'), function () {
            workspace.window.showMask();
            Gikam.postText(limsLocationBoard.baseUrl + "/clean-samples", Gikam.getJsonWrapper({}, [null, selections])).done(function (data) {
                grid.refresh();

                var boardGrid = Gikam.getComp('lims-storage-location-board-choose-list-grid');
                var activedBoardRow = boardGrid.getActivedRow();
                boardGrid.refreshRowById(activedBoardRow.id);
                limsLocationBoard.refreshHole(activedBoardRow.id);
            }).always(function () {
                workspace.window.closeMask();
            });
        });
    },

    getChooseListColumns: function () {
        return [{index: true},{
            field: 'boardCode',
            title: 'T_LIMS_LOCATION_BOARD.BOARDCODE',
            width: 140
        }, {
            field: 'boardName',
            title: 'T_LIMS_LOCATION_BOARD.BOARDNAME',
            width: 140
        }, {
            field: 'ext$.locationdesc',
            title: 'LIMS.TAB.LOCATION.BOARD',
            width: 250
        }, {
            field: 'freeHoleCount',
            title: 'T_LIMS_LOCATION_BOARD.FREEHOLECOUNT',
            width: 140
        }, {
            field: 'boardType',
            title: 'T_LIMS_LOCATION_BOARD.BOARDTYPE',
            type: 'select',
            category: 'sys',
            firstBlank: false,
            param: {
                codeCategoryId: 'boardType'
            },
            width: 140
        }, {
            field: 'rowNum',
            title: 'T_LIMS_LOCATION_BOARD.ROWNUM',
            width: 100
        }, {
            field: 'columnNum',
            title: 'T_LIMS_LOCATION_BOARD.COLUMNNUM',
            width: 100
        }];
    },

    getGridParam: function () {
        var _this = this, columns = _this.getChooseListColumns();
        columns.unshift({
            radio: true
        });
        return {
            type: 'grid',
            id: 'lims-storage-location-board-choose-list-grid',
            url: limsLocationBoard.baseUrl + '/queries/choosable',
            page: true,
            columns: columns,
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0);
                } else {
                    Gikam.cleanCompData(['sample-storage-Location-choose']);
                }
            },
            onRowActive: function (index, row) {
                _this.refreshHole(row.id);
            },
            requestData: {
                createdByOrgId_EQ: workspace.user.ext$.loginorgid
            }
        }
    },

    refreshHole: function (id) {
        Gikam.postSync(limsLocationBoard.holeUrl + '/queries', Gikam.getJsonWrapper({
            f: {
                boardId_EQ: id
            },
            o: [{
                y : 'asc'
            },{
                x : 'asc'
            }],
            n: -1, s: -1
        })).done(function (data) {
            const  iframeDom=document.querySelector("#ksh-storage-location-board-iframe >iframe");
            let flag=false;  //是否初次加载

            iframeDom.onload=function(){
                flag =true;
                iframeDom.contentWindow.postMessage({
                    type:1,
                    data: data.rows
                }, "*");
            };
            if(!flag){
                iframeDom.contentWindow.postMessage({
                    type:1,
                    data: data.rows
                }, "*");
            }
        });
    },

    create: function () {
        var _this = this;
        Gikam.create('layout', {
            id: 'lims-storage-location-sample-board-choose-list-layout',
            renderTo: Gikam.getLastModal().window.$dom,
            west: {
                title : 'LIMS.TAB.LOCATION.BOARD',
                width: '50%',
                items: [{
                    type: 'layout',
                    center: {
                        items: [this.getSampleGrid()]
                    },
                    south: {
                        height: '50%',
                        title: 'T_LIMS_LOCATION_BOARD.TITLE.BOARD',
                        items: [this.getGridParam()]
                    }
                }]
            },
            center: {
                items: [{
                    type: 'iframe',
                    src : '/static/basemodule/samplemanage/assign-location/index.html',
                    id : 'ksh-storage-location-board-iframe'
                }]
            }
        });
    },

    init: function (param) {
        this.param = param;
        this.create();
    }
};
